package com.cwj.itextpdf;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

/**
 * @author wujie.chen
 * 2021/9/14 11:42
 */
public class PdfTest {
    public static void main(String[] args) {
        String tmpPath = System.getProperty("java.io.tmpdir");
        System.out.println(tmpPath);
    }

    public void watermark() throws IOException, DocumentException {
        PdfReader reader = new PdfReader("src/main/resources/task.pdf");
        OutputStream outputStream = new FileOutputStream(new File("src/main/resources/task_watermark.pdf"));
        PdfStamper pdfStamper = new PdfStamper(reader, outputStream);

        int numberOfPages = reader.getNumberOfPages();
        BaseFont bfChinese = BaseFont.createFont();
        PdfGState gState = new PdfGState();
        gState.setFillOpacity(0.5f);
        for (int i = 1; i <= numberOfPages; i++) {
            PdfContentByte overContent = pdfStamper.getOverContent(i);
            overContent.beginText();
            overContent.setGState(gState);
            overContent.setRGBColorFill(169, 169, 169);
            overContent.setFontAndSize(bfChinese, 30);
            overContent.showTextAligned(Element.ALIGN_BOTTOM, "watermark", 50, 50, 50);
            overContent.endText();
            System.out.println(i);
        }
        pdfStamper.close();
        outputStream.flush();
        outputStream.close();
    }
}
